﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com
 * 
 * */

using System;
using System.Threading;
using System.Windows.Forms;
using QWModel;
using QWRepository;
using QWRepository.Local;

namespace QWScreenPanel
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            bool isConnected = false;
            int nCount = 0;

            // show Waiting 
            using (var fsplash = new QWaitingWidget("Version " + 
                Application.ProductVersion + "\r\n\r\n" +
                "Preparing application\r\nPlease wait!"))
            {
                fsplash.WindowState = FormWindowState.Maximized;
                fsplash.Show();
                fsplash.SetBigCircle();
                Application.DoEvents();

                // 5 phase before setting);
                while (!isConnected && nCount < 3)
                {
                    // connecting
                    isConnected = IsConnected(false);
                    nCount++;

                    if (!isConnected)
                    {
                        // set splash message
                        fsplash.SetMessage(
                            string.Format("Connection failed! Re-trying connecting to server\r\nPHASE {0}", nCount));
                        Application.DoEvents();

                        // delay
                        for (int i = 0; i <= 250; i++)
                        {
                            Thread.Sleep(10);
                            Application.DoEvents();
                        }
                    }
                    else
                    {
                        fsplash.SetMessage(string.Format("Connection successfully !"));
                        Application.DoEvents();
                        Thread.Sleep(250);
                    }
                }
            }

            // if still not connected, show db configuration
            if (!isConnected)
                using (var f = new DBLogonUI())
                    f.ShowDialog();
            else // in connected show main screen
                Application.Run(new MainScreen());
        }

        /// <summary>
        /// Determines whether the specified b MSG is connected.
        /// </summary>
        /// <param name="bMsg">if set to <c>true</c> [b MSG].</param>
        /// <returns>
        /// 	<c>true</c> if the specified b MSG is connected; otherwise, <c>false</c>.
        /// </returns>
        private static bool IsConnected(bool bMsg)
        {
            try
            {
                var db1 = new WorkflowBase();
                return true;
            }
            catch (Exception ex)
            {
                if (bMsg)
                    MessageBox.Show(
                        string.Format(ex.Message), @"Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }
    }
}